/**
 * Task table.
 */
var taskTable = {};
var prefix = "o:t:";
var s_key = "o:t:s";
var prefix_p = "o:t:p:";
var prefix_v = "o:t:v:";

function gettime() {
	"use strict";
	var d = new Date();
	return d.toUTCString();
}

function defaultCallback(err) {
	if (err) {
		console.error("taskTable redis error:" + err);
	}
}

taskTable.add = function(context, task, callback) {
	"use strict";
	var redis = context.redis;
	if (!callback) {
		callback = defaultCallback;
	}

	console.error("add new task:");
	console.error(task);
	
	var key = prefix + task.id;

	task.createtime = gettime();

	context.beginMulti();
	redis.sadd(s_key, task.id, defaultCallback);
	redis.hmset(key, task, callback);
	context.endMulti(defaultCallback);
};

taskTable.load = function(context, id, callback) {
	"use strict";
	var redis = context.redis;
	if (!callback) {
		callback = defaultCallback;
	}
	
	var key = prefix + id;
	redis.hgetall(key, callback);
};

taskTable.loadFields = function(context, id, fields, callback) {
	"use strict";
	var redis = context.redis;
	
	var key = prefix + id;

	redis.hmget(key, fields, callback);
};

taskTable.del = function(context, id, callback) {
	"use strict";
	var redis = context.redis;
	if (!callback) {
		callback = defaultCallback;
	}
	
	var key = prefix + id;
	context.beginMulti();
	redis.srem(s_key, id, defaultCallback);
	redis.del(prefix_p+id, defaultCallback);
	redis.del(prefix_v+id, defaultCallback);
	redis.del(key, callback);
	context.endMulti(defaultCallback);
};

taskTable.scan = function(context, cursor, count, callback) {
	"use strict";
	var redis = context.redis;
	if (!callback) {
		callback = defaultCallback;
	}
	
	redis.sscan([s_key, cursor, "COUNT", count], callback);
};

taskTable.count = function(context, callback) {
	"use strict";
	var redis = context.redis;
	if (!callback) {
		callback = defaultCallback;
	}
	
	redis.scard(s_key, callback);
};


taskTable.add_p = function(context, taskId, groupId, callback) {
	"use strict";
	var redis = context.redis;
	if (!callback) {
		callback = defaultCallback;
	}
	
	var field_name = groupId;
	var key = prefix_p + taskId;
	redis.sadd(key, field_name, callback);
};

taskTable.add_v = function(context, taskId, groupId, callback) {
	"use strict";
	var redis = context.redis;
	if (!callback) {
		callback = defaultCallback;
	}
	
	var field_name = groupId;
	var key = prefix_v + taskId;
	redis.sadd(key, field_name, callback);
};

taskTable.del_p = function(context, taskId, groupId, callback) {
	"use strict";
	var redis = context.redis;
	if (!callback) {
		callback = defaultCallback;
	}
	
	var field_name = groupId;
	var key = prefix_p + taskId;
	redis.srem(key, field_name, callback);
};

taskTable.del_v = function(context, taskId, groupId, callback) {
	"use strict";
	var redis = context.redis;
	if (!callback) {
		callback = defaultCallback;
	}
	
	var field_name = groupId;
	var key = prefix_v + taskId;
	redis.srem(key, field_name, callback);
};

taskTable.getAll_p = function(context, taskId, callback) {
	"use strict";
	var redis = context.redis;
	if (!callback) {
		callback = defaultCallback;
	}

	var key = prefix_p + taskId;
	redis.smembers(key, callback);
};

taskTable.getAll_v = function(context, taskId, callback) {
	"use strict";
	var redis = context.redis;
	if (!callback) {
		callback = defaultCallback;
	}

	var key = prefix_v + taskId;
	redis.smembers(key, callback);
};

taskTable.count_p = function(context, taskId, callback) {
	"use strict";
	var redis = context.redis;
	if (!callback) {
		callback = defaultCallback;
	}

	var key = prefix_p + taskId;
	redis.scard(key, callback);
};

taskTable.count_v = function(context, taskId, callback) {
	"use strict";
	var redis = context.redis;
	if (!callback) {
		callback = defaultCallback;
	}

	var key = prefix_v + taskId;
	redis.scard(key, callback);
};

taskTable.exist_p = function(context, taskId, groupId, callback) {
	"use strict";
	var redis = context.redis;
	if (!callback) {
		callback = defaultCallback;
	}

	var key = prefix_p + taskId;
	var field_name = groupId;
	
	redis.sismember(key, field_name, callback);
};

taskTable.exist_v = function(context, taskId, groupId, callback) {
	"use strict";
	var redis = context.redis;
	if (!callback) {
		callback = defaultCallback;
	}

	var key = prefix_v + taskId;
	var field_name = groupId;
	
	redis.sismember(key, field_name, callback);
};

module.exports = taskTable;
